var pagNum = 0;
var userId = userid;
var userName = localStorage.getItem("username");
jQuery(function(){
    jQuery.noConflict();
    if(userId ==null || userId == ""){
        window.location.href=server_url+"login.do";
    }
    var nav_list = Zepto("#nav_list li");
    var sec = Zepto("section");
    nav_list.bind("click",function(){
        Zepto(this).siblings('li').css({"color":"#9f9f9f"});
        Zepto(this).css({"color":"#89bafe"});
        sec.css({"display":"none"})
        sec.eq(Zepto(this).index()).css({"display":"block"});
        if($(this).index()==2){
            pagNum =0;
            jQuery("#trend").children().remove();
            loadTrend();
        }
    });
    $(".Swrap").scroll(function () {
        if ($(".Swrap").scrollTop() + $(window).height()+1 >=$("#trend").height()) {
            loadTrend();
        }
    });
});
var b = false;
var loaded = false;
var loadTrend = function (){
    jQuery("#next_page").remove();
    jQuery.ajax({
        url:server_url+"/rest/sellerOrder/trend.do",
        data:{sellerUserId:userid,pageNum:pagNum},
        success:function(data){
            console.log(data);
            if(data.data.length>0){
                mapIfExist(function(order){
                    //console.log(order);
                    var status=(order.orderStatus==='01' || order.orderStatus==='05')?order.orderStatusName:order.shipStatusName;
                    var detailHtml = _.map(order.lstOrderDetail,function(detail){
                        //console.log(detail);
                        return '<div class="ware">\
										<h4 class="clear">\
										<a href="javascript:;">'+detail.productName+" "+detail.spec+'</a>\
										<span>'+detail.productPrice+' RMB/头</span>\
									</h4>\
									<p class="proNumber clear">\
										<span>'+parseFloat(detail.productNum)+'头</span>\
									</p>\
								</div>';
                    }).join("");
                    var operation='';
                    jQuery('<div class="d_order" >\
							<h3>\
								<a href="javascript:;">链牛买家</a>\
							</h3>'+
                    detailHtml+
                    '<p class="all"><span>合计：￥'+order.price+'</span></p>'+
                    operation+
                    '<p class="split"></p>\
                </div>').appendTo(jQuery("#trend"));
                }, data.data);
                jQuery('<div id="next_page" class="d_order" style="text-align: center">\
								<a href="javascript:loadTrend();">更多</a>\
							').appendTo(jQuery("#trend"));
                loaded = true;
            }else{
                if(loaded){
                    jQuery('<div id="next_page" class="d_order" >\
								<a href="javascript:;">没有更多了</a>\
								<span>'+status+'</span>\
							').appendTo(jQuery("#trend"));
                }
                else {
                    jQuery("<div>" +
                    "<p style='font-size:0.35rem; padding:50% 5% 50% 5%; text-align:center;'>您暂时还没有交易订单!</p>" +
                    "</div>").appendTo(Zepto("#trend"));
                }
            }
            pagNum++;
            //initPage(orderStatus,shipStatus);
        }
    });
}
function initPage(orderStatus,shipStatus){
    var state = Zepto("#state p");
    if(orderStatus =='02'&&shipStatus =='03'){
        Zepto(state[4]).siblings('p').removeClass("select");
        Zepto(state[4]).addClass("select");
    }else if(orderStatus ==''&&shipStatus =='01'){
        Zepto(state[3]).siblings('p').removeClass("select");
        Zepto(state[3]).addClass("select");
    }else if(orderStatus =='02'&&shipStatus =='02'){
        Zepto(state[2]).siblings('p').removeClass("select");
        Zepto(state[2]).addClass("select");
    }else if(orderStatus =='01'&&shipStatus ==''){
        Zepto(state[1]).siblings('p').removeClass("select");
        Zepto(state[1]).addClass("select");
    }else{
        Zepto(state[0]).siblings('p').removeClass("select");
        Zepto(state[0]).addClass("select");
    }

    var nav_list = Zepto("#nav_list li");
    var sec = Zepto("section");
    nav_list.bind("click",function(){
        Zepto(this).siblings('li').css({"color":"#9f9f9f"});
        Zepto(this).css({"color":"#89bafe"});
        sec.css({"display":"none"})
        sec.eq(Zepto(this).index()).css({"display":"block"});

    });
    Zepto(".cancel").tap(function(){
        Zepto("#wrap_order").css({"display":"block"})
        jQuery("#cancelOrderId").val(jQuery(this).attr("orderid"));
        jQuery("#overPlusNum").val(jQuery(this).attr("productNum"));
        jQuery("#skuId").val(jQuery(this).attr("skuId"));
    });
    Zepto("#close_icon").tap(function(){
        Zepto("#wrap_order").css({"display":"none"})
    });


    Zepto("#problem_text").tap(function(){
        Zepto("#problem").css({"display":"block"})
    });
    Zepto("#close_text").tap(function(){
        Zepto("#problem").css({"display":"none"})
    });
    Zepto("#hold").tap(function(){
        Zepto("#problem").css({"display":"none"})
    });


}